﻿CREATE PROCEDURE [dbo].[ShiftInLinesDelete]
(
	@Original_Id int,      --CD.InputLines.Id
	@Original_MD datetime	 --CD.InputLines.MD
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @intPartyId int
	SET NOCOUNT ON

SELECT @intPartyId = P.Id
FROM CD.InputLines I LEFT OUTER JOIN CD.Parties P ON I.Id = P.InputLineId AND I.DocId = P.DocId
WHERE I.Id = @Original_Id AND I.MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 1, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

BEGIN TRAN

IF @intPartyId Is NOT Null
BEGIN
		EXEC @intRet = dbo.PartyGoodsDelete @intPartyId, 1
		IF @intRet <> 0 
		BEGIN
			IF @@trancount > 0 ROLLBACK TRAN
			RETURN @intRet
		END
END

	SET NOCOUNT OFF;
DELETE FROM CD.InputLines WHERE Id = @Original_Id

COMMIT TRAN

RETURN 0
